class ElementsController < ApplicationController
  active_scaffold :element do |config|
    config.list.per_page = 10
    config.list.sorting = {:id => :asc}
    config.label = "要件リスト"
    config.columns = [:taggings, :title, :description]
    config.columns[:title].label = "タイトル"
    config.columns[:title].description = "要件の題名を必ず入力してください。"
    config.columns[:title].required = true
    config.columns[:description].label = "内容"
    config.columns[:description].description = "要件の説明を入力してください。"
    config.columns[:taggings].label = "タグ"
    config.columns[:tags].label = "タグ"
    config.columns[:tags].description = "要件を表すタグを0個以上選んでください。"
    config.columns[:tags].form_ui = :select
    config.show.columns = [:id, :title, :description, :taggings, :created_at, :updated_at]
    config.update.columns = [:title, :description, :tags]
    config.create.columns = [:title, :description, :tags]
    config.action_links.add 'index', :controller => :tag, :label => '★タグ編集'
    config.action_links.add 'history', :type => :record, :label => 'History'
  end
  
  layout "activescaffold"
  
  protected
  
  def after_update_save(record)
    record.update_tags(tag_id_param_list())
  end

  def after_create_save(record)
    record.update_tags(tag_id_param_list())
  end

  def tag_id_param_list
    h = params[:record][:tags]
    h ? h.values.map{|r| Tag.find(r["id"].to_i).name} : []
  end
  
end
